<?php
/**
 * 管理中心· 权限管理模块的逻辑处理
 * ============================================================================
 * 版权所有 2017-2020 北京优特世纪科技有限公司，并保留所有权利。
 * 网站地址: http://www.unitewww.com;
 * ============================================================================
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * @since: 1.0
 * @version: 1.0
 **/
namespace Hoteladmin\Model;
use Think\Model;
class AdminModel extends Model{
	/**
	 +--------------------------------------------------------------------------
	 * 实现管理员登录的功能
	 * @param type $uname 用户名
	 * @param type $upwds 密码
	 * @author:  XC跃
	 * @date: 2018-03-09
	 +--------------------------------------------------------------------------
	 */
	public function login($uname,$upwds){
	    //实例化相关表
	    $aident = M('admin');
	    $result = array();
	    if(!$uname || !$upwds){
	        $result= array('status'=>-1,'msg'=>'请填写账号或密码');
	    }
	    $user = $aident->where("(`ad_name`='".$uname."') or (`ad_phone`='".$uname."')")->find();
	    if(!$user){
	        $result = array('status'=>-1,'msg'=>'账号不存在!');
	    }elseif($upwds != $user['ad_pwd']){
	        $result = array('status'=>-1,'msg'=>'用户密码错误!');
	        addLog($user['ad_name'],'3','2','登录密码错误');
	    }elseif($user['ad_state'] == '2'){
	        $result = array('status'=>-1,'msg'=>'该账户已被锁定，请联系管理员！');
	    }else{
	        //登陆成功后记录相关属性
	        $data = array();
	        $data['ad_id']   = $user['ad_id'];
	        $data['ad_logip']= get_client_ip();
	        $data['ad_ltime']= time();
	        $data['ad_count']= array('exp','ad_count+1');
	        $aident->save($data);
	        $result = array('status'=>1,'msg'=>'登陆成功','result'=>$user);
	    }
	    return $result;
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现会员登录的功能
	 * @param type $uname 用户名或手机号
	 * @param type $upwds 密码
	 * @author:  XC跃
	 * @date: 2018-03-17
	 +--------------------------------------------------------------------------
	 */
	public function user_login($uname,$upwds,$openid=null){
	    //实例化相关表
	    $usident = M('user');
	    $result = array();
	    if(!$uname || !$upwds){
	        $result= array('status'=>-1,'msg'=>'请填写账号或密码');
	    }
	    $user = $usident->where("(`us_uid`='".$uname."') or (`us_phone`='".$uname."')")->find();
	    if(!$user){
	        $result = array('status'=>-1,'msg'=>'账号不存在!');
	    }elseif($upwds != $user['us_pwd']){
	        $result = array('status'=>-1,'msg'=>'用户密码错误!');
	        addLog($user['us_uid'],'3','1','登录密码错误');
	    }elseif($user['us_state'] == '2'){
	        $result = array('status'=>-1,'msg'=>'该账户已被锁定，请联系管理员！');
	    }else{
	        //登陆成功后记录相关属性
	        $data = array();
	        $data['us_id']     = $user['us_id'];
	        $data['us_ip']     = get_client_ip();
	        $data['us_ltime']  = time();
//            $openid = getOpenid();
//
//            $data['openid']  = $openid;

//	        $data['session_id']= session_id();
	        $usident->save($data);
	        $result = array('status'=>1,'msg'=>'登陆成功','result'=>$user);
	    }
	    return $result;
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现会员自主注册的功能
	 * @param $pid   推荐人ID
	 * @param $phone 注册手机号
	 * @param $reg   系统参数
	 * @author: XC跃
	 * @date : 2018-03-19
	 +--------------------------------------------------------------------------
	 */
	public function user_regist($pid,$phone,$sms){
	    //实例化相关表
	    $usident = M('user');
			$dlident = M('daili');
			$reident = M('region');
		  $type = I('get.type', 'sIno');
		  $conf = tpCache($type);
	    //1、添加注册信息	    
	    $data['us_uid']  = $uid = rand_number(8);
	    $data['us_pid']  = $pid;
	    $data['us_gid']  = '1';
	    $data['us_name'] = $uname = trim(I('post.uname'));
	    $data['us_phone']= $phone;
//        $openid = getOpenid();
//
//        $data['openid']  = $openid;
        $data['us_area']  = trim(I('post.area'));
	    $data['us_pwd']  = $pwds = md5(trim(I('post.pwds')));
	    $data['us_pwds'] = $pwds;
	    $data['us_atime']= time();
	    $data['us_state']= '3';  //1已审核 2锁点 3待激活4待认证 5拒绝
	    $ure = $usident->add($data);
		  accShopLog($data['us_uid'],0,$conf['registers'],$conf['register'],0,0, '会员注册赠送');
	    //3、更新推荐人数
	    $usident->where("`us_uid`='".$pid."'")->setInc('us_count',1);
	    accShopLog($pid,0,$conf['tuiregs'],$conf['tuireg'],0, 0,'推荐会员注册赠送');
	    $ure>0?$res=1:$res=0;
	    return $res;
	}
	/**
	 +--------------------------------------------------------------------------
	 * 实现首次生成二维码
	 * @author: XC跃
	 * @date:2018-03-20
	 +--------------------------------------------------------------------------
	 */
	public function first_login($uid){
	    //实例化相关表
	    $usident = M('user');
	    //获取数据
	    $usFind = $usident->where("`us_uid`='".$uid."'")->field('us_pid,us_phone,us_jtime,us_state')->find();
	    $phone= $usFind['us_phone'];
	    if('3'==$usFind['us_state']){
	        //1、更改会员状态
	        $jdata['us_jtime'] = time();  //激活时间
	        $jdata['us_state'] = '1';     //状态改为正常
	        $usident->where("`us_uid`='".$uid."'")->save($jdata);
	        //2、生成推广二维码
	        $host = "http://".$_SERVER['SERVER_NAME'];
	        $desc = $host."/Home/Login/register?u=".$phone;
	        $name = $phone.".png";
	        $file = "QRcode";
	        qrcode($desc,$file,$name);
	    }
	}
	/**
	 +--------------------------------------------------------------------------
	 * 管理欢迎页· 获取服务器配置参数
	 * @author: XC跃
	 * @date ：2017-04-01
	 +--------------------------------------------------------------------------
	 */
	public function get_sys_info(){
	    $sys_info['os']             = PHP_OS;
	    $sys_info['zlib']           = function_exists('gzclose') ? 'YES' : 'NO';//zlib
	    $sys_info['safe_mode']      = (boolean) ini_get('safe_mode') ? 'YES' : 'NO';//safe_mode = Off
	    $sys_info['timezone']       = function_exists("date_default_timezone_get") ? date_default_timezone_get() : "no_timezone";
	    $sys_info['curl']			= function_exists('curl_init') ? 'YES' : 'NO';
	    $sys_info['web_server']     = $_SERVER['SERVER_SOFTWARE'];
	    $sys_info['phpv']           = phpversion();
	    $sys_info['ip'] 			= GetHostByName($_SERVER['SERVER_NAME']);
	    $sys_info['fileupload']     = @ini_get('file_uploads') ? ini_get('upload_max_filesize') :'unknown';
	    $sys_info['max_ex_time'] 	= @ini_get("max_execution_time").'s'; //脚本最大执行时间
	    $sys_info['set_time_limit'] = function_exists("set_time_limit") ? true : false;
	    $sys_info['domain'] 		= $_SERVER['HTTP_HOST'];
	    $sys_info['memory_limit']   = ini_get('memory_limit');
	    if(function_exists("gd_info")){
	        $gd = gd_info();
	        $sys_info['gdinfo'] 	= $gd['GD Version'];
	    }else {
	        $sys_info['gdinfo'] 	= "未知";
	    }
	    return $sys_info;
	}

	/**
	+--------------------------------------------------------------------------
	 * 实现会员自主注册的功能
	 * @param $pid   推荐人ID
	 * @param $phone 注册手机号
	 * @param $reg   系统参数
	 * @author: XC跃
	 * @date : 2018-03-19
	+--------------------------------------------------------------------------
	 */
	public function shiFang($orFind){
		$orident = M('order_details');
		$usident = M('user');
		$prident = M('prop');
		$grident = M('grade');
		//商品详情
		$detail = $orident->where("`de_oid`='".$orFind['or_id']."'")->find();
		//商品
		$prop = $prident->where("`gp_id`='".$detail['de_gid']."'")->find();
		//用户
		$usFind = $usident->where("`us_uid`='".$orFind['or_uid']."'")->find();
		//等级
		$grade = $grident->where("`gr_id`='".$prop['gp_gid']."'")->find();
		if($grade['gr_id']>$usFind['us_gid']){
			$usFind = $usident->where("`us_uid`='".$orFind['or_uid']."'")->setField('us_gid',$grade['gr_id']);
		}
		//运营中心报单补助
		$bzmoey=round($prop['gp_price']*0.03,0);
		if($usFind['us_pid']!=NULL){
			do_buzhu($usFind['us_pid'],$bzmoey);
		}
//		$buzhu = $usident->where("`us_gid`='4'")->field('us_uid')->select();
//		foreach($buzhu as $key=>$val){
//			accShopLog($val['us_uid'],$bzmoey,$bzmoey*-1,'0','报单补助');
//		}
		accShopLog($usFind['us_uid'],0,$grade['gr_frozen'],0,0,0,'自主兑换商品');
		if($usFind['us_pid']!=NULL){
			do_smoney($usFind['us_pid'],$grade['gr_money']);
		}

		//直推一代
		$usFind1 = $usident->where("`us_uid`='".$usFind['us_pid']."'")->find();
		if($usFind['us_state']!='2'){
				$usmoney1 = $usFind1['us_points']-$grade['gr_recom1'];
				$usMon1 = $usmoney1>=0?$grade['gr_recom1']:$usFind1['us_points'];
			}
			//如果积分足
		if($usFind1!=NULL){
			if($usmoney1>0) {
				accShopLog($usFind1['us_uid'], $grade['gr_recom1'],-$grade['gr_recom1'], '0', '0', 0,'直推一代释放');
				//如果积分不够
			} else{
				accShopLog($usFind1['us_uid'], $usMon1,-$usMon1, '0', '0',0, '直推1代静态释放');
				//直推2代差额静态释放
				//获取推荐人信息
				$user1= $usident->where("`us_uid`='".$usFind1['us_pid']."'")->find();
				if($user1!=NULL){
					if($user1['us_state']!='2'){
						$usmoney2 = $user1['us_points']-abs($usmoney1);
						$upMon1 = $usmoney2>=0?abs($usmoney1):$user1['us_points'];
						if($usmoney2>0){
							accShopLog($user1['us_uid'], $upMon1, -$upMon1, '0','0',0, '直推2代差额静态释放');
						} else {
							accShopLog($user1['us_uid'], $upMon1,-$upMon1, '0', '0',0, '直推2代差额静态释放');
							//直推3代差额静态释放
							$user2 = $usident->where("`us_uid`='" . $user1['us_pid'] . "'")->find();
							if ($user2['us_state'] != '2') {
								$usmoney3 = $user2['us_points'] - abs($usmoney2);//
								$upMon2 = $usmoney3 >= 0 ? abs($usmoney2) : $user2['us_points'];//需要减去的
								if ($usmoney3 > 0) {
									accShopLog($user2['us_uid'], $upMon2,-$upMon2,'0', 0, '0', '直推3代差额静态释放');
								}else{
									accShopLog($user2['us_uid'], $upMon2,-$upMon2, '0',0, '0', '直推3代差额静态释放');
									//直推4代差额静态释放
									$user3 = $usident->where("`us_uid`='" . $user2['us_pid'] . "'")->find();
									if ($user3['us_state'] != '2') {
										$usmoney4 = $user3['us_points'] - abs($usmoney3);//
										$upMon3 = $usmoney4 >= 0 ? abs($usmoney3) : $user3['us_points'];//需要减去的
										if ($usmoney4 > 0) {
											accShopLog($user3['us_uid'], $upMon3, -$upMon3,'0', '0', '直推4代差额静态释放');
										}else{
											accShopLog($user3['us_uid'], $upMon3,-$upMon3,'0',  '0', '直推4代差额静态释放');
											//直推5代差额静态释放
											$user4 = $usident->where("`us_uid`='" . $user3['us_pid'] . "'")->find();
											if ($user4['us_state'] != '2') {
												$usmoney5 = $user4['us_points'] - abs($usmoney4);//
												$upMon4 = $usmoney5 >= 0 ? abs($usmoney4) : $user4['us_points'];//需要减去的
												if ($usmoney5 > 0) {
													accShopLog($user4['us_uid'], $upMon4,-$upMon4,'0',  '0', '直推5代差额静态释放');
												}
											}
										}
									}
								}
							}
						}
					}
				}else{
					return;
				}
			}
		}else{
			return;
		}

		//直推二代
		$usFind2 = $usident->where("`us_uid`='".$usFind1['us_pid']."'")->find();
		$grade2= $grident->where("`gr_id`='".$usFind2['us_gid']."'")->find();
		$money2=round($grade2['gr_recom2']*$grade['gr_recom1']*0.01,2);
		//		accShopLog($usFind3['us_uid'],$money3,-$money3,'0','直推二代释放');
		if($usFind2['us_state']!='2') {
			$us2money1 = $usFind2['us_points']-$money2;
			$us2Mon1 = $us2money1>=0?$money2:$usFind2['us_points'];
		}
	  //如果积分足
		if($us2money1>0) {
				accShopLog($usFind2['us_uid'],$us2Mon1,-$us2Mon1, '0', '0', '直推二代释放');
			//如果积分不够
			} else{
				accShopLog($usFind2['us_uid'], $us2Mon1,-$us2Mon1,'0',  '0', '直推2代静态释放');
			//直推2代差额静态释放
				//获取推荐人信息
				$user1= $usident->where("`us_uid`='".$usFind2['us_pid']."'")->find();
				if($user1['us_state']!='2'){
					$usmoney2 = $user1['us_points']-abs($us2money1);
					$upMon1 = $usmoney2>=0?abs($us2Mon1):$user1['us_points'];
					if($usmoney2>0){
						accShopLog($user1['us_uid'], $upMon1,-$upMon1,  '0','0', '直推3代差额静态释放');
					} else {
						accShopLog($user1['us_uid'], $upMon1, -$upMon1,'0', '0', '直推3代差额静态释放');
					//直推3代差额静态释放
						$user2 = $usident->where("`us_uid`='" . $user1['us_pid'] . "'")->find();
						if ($user2['us_state'] != '2') {
							$usmoney3 = $user2['us_points'] - abs($usmoney2);//
							$upMon2 = $usmoney3 >= 0 ? abs($usmoney2) : $user2['us_points'];//需要减去的
							if ($usmoney3 > 0) {
								accShopLog($user2['us_uid'], $upMon2, -$upMon2,'0', '0', '直推4代差额静态释放');
							}else{
								accShopLog($user2['us_uid'], $upMon2, -$upMon2,'0', '0', '直推4代差额静态释放');
								//直推4代差额静态释放
								$user3 = $usident->where("`us_uid`='" . $user2['us_pid'] . "'")->find();
								if ($user3['us_state'] != '2') {
									$usmoney4 = $user3['us_points'] - abs($usmoney3);//
									$upMon3 = $usmoney4 >= 0 ? abs($usmoney3) : $user3['us_points'];//需要减去的
									if ($usmoney4 > 0) {
										accShopLog($user3['us_uid'], $upMon3,-$upMon3, '0', '0', '直推5代差额静态释放');
									}
								/*	else{
										accShopLog($user3['us_uid'], $upMon3, -$upMon3,'0', '0', '直推5代差额静态释放');
										//直推5代差额静态释放
										$user4 = $usident->where("`us_uid`='" . $user3['us_pid'] . "'")->find();
										if ($user4['us_state'] != '2') {
											$usmoney5 = $user4['us_points'] - abs($usmoney4);//
											$upMon4 = $usmoney5 >= 0 ? abs($usmoney4) : $user4['us_points'];//需要减去的
											if ($usmoney5 > 0) {
												accShopLog($user4['us_uid'], $upMon4, $upMon4 * -1,'0', '0', '直推5代差额静态释放');
											}
										}
									}*/
								}
							}
						}
					}
				}
			}
		//直推三代
		$usFind3 = $usident->where("`us_uid`='".$usFind2['us_pid']."'")->find();
		$grade3= $grident->where("`gr_id`='".$usFind3['us_gid']."'")->find();
		$money3= round($grade3['gr_recom2']*$money2*0.01,2);
		//		accShopLog($usFind3['us_uid'],$money3,-$money3,'0','直推三代释放');
		if($usFind3['us_state']!='2') {
			$us3money1 = $usFind3['us_points']-$money3;
			$us3Mon1 = $us3money1>=0?$money3:$usFind3['us_points'];
		}
		if($us3money1>0) {
			accShopLog($usFind3['us_uid'],$us3Mon1, -$us3Mon1,'0', '0', '直推三代释放');
			//如果积分不够
		}else{
			accShopLog($usFind3['us_uid'], $us3Mon1,-$us3Mon1,'0',  '0', '直推3代静态释放');
			//获取推荐人信息
			$user1= $usident->where("`us_uid`='".$usFind3['us_pid']."'")->find();

			if($user1['us_state']!='2'){
				$usmoney2 = $user1['us_points']-abs($us3money1);
				$upMon1 = $usmoney2>=0?abs($us3Mon1):$user1['us_points'];
				if($usmoney2>0){
					accShopLog($user1['us_uid'], $upMon1,-$upMon1,'0',  '0', '直推4代差额静态释放');
				} else {
					accShopLog($user1['us_uid'], $upMon1, -$upMon1, '0','0', '直推4代差额静态释放');
					//直推3代差额静态释放
					$user2 = $usident->where("`us_uid`='" . $user1['us_pid'] . "'")->find();
					if ($user2['us_state'] != '2') {
						$usmoney3 = $user2['us_points'] - abs($usmoney2);//
						$upMon2 = $usmoney3 >= 0 ? abs($usmoney2) : $user2['us_points'];//需要减去的
						if ($usmoney3 >0){
							accShopLog($user2['us_uid'], $upMon2, -$upMon2,'0', '0', '直推5代差额静态释放');
						}
					}
				}
			}
		}
//		$ure>0?$res=1:$res=0;
//		return $res;
	}



	
	
}